﻿





using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;

using KDS.Mes.Mes.StopEquipment.Dtos;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.StopEquipments;
using KDS.Mes.Mes.StopEquipments.Dtos;
using KDS.Mes.Dto;



#region 代码生成器相关信息_ABP Code Generator Info
//你好，我是ABP代码生成器的作者,欢迎您使用该工具，目前接受付费定制该工具，有需要的可以联系我
//我的邮箱:werltm@hotmail.com
// 官方网站:"http://www.yoyocms.com"
// 交流QQ群：104390185  
//微信公众号：角落的白板报
// 演示地址:"vue版本：http://vue.yoyocms.com angularJs版本:ng1.yoyocms.com"
//博客地址：http://www.cnblogs.com/wer-ltm/
//代码生成器帮助文档：http://www.cnblogs.com/wer-ltm/p/5777190.html
// <Author-作者>梁桐铭 ,微软MVP</Author-作者>
// Copyright © YoYoCms@China.2017-11-29T07:44:00. All Rights Reserved.
//<生成时间>2017-11-29T07:44:00</生成时间>
#endregion


namespace KDS.Mes.Mes.StopEquipments
{
    /// <summary>
    /// 停机信息服务实现
    /// </summary>
   [AbpAuthorize(AppPermissions.Pages_StopEquipment)]
		 
       
    public class StopEquipmentAppService : MesAppServiceBase, IStopEquipmentAppService
    {
        private readonly IRepository<StopEquipment,int> _stopEquipmentRepository;
        private readonly IStopEquipmentsListExcelExporter _stopEquipmentExcelExporter;


        private readonly StopEquipmentManage _stopEquipmentManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public StopEquipmentAppService( IRepository<StopEquipment,int> stopEquipmentRepository, IStopEquipmentsListExcelExporter stopEquipmentListExcelExporter,
StopEquipmentManage stopEquipmentManage
  )
        {
            _stopEquipmentRepository = stopEquipmentRepository;
			 _stopEquipmentManage = stopEquipmentManage;
            _stopEquipmentExcelExporter = stopEquipmentListExcelExporter;

        }


		#region 实体的自定义扩展方法
        private IQueryable<StopEquipment> _stopEquipmentRepositoryAsNoTrack => _stopEquipmentRepository.GetAll().AsNoTracking();


        #endregion

        #region 停机信息管理

        public PagedResultDto<StopEquipmentListDto> GetPagedStopEquipments(GetStopEquipmentInput input)
        {
            ////初步过滤
            var query = _stopEquipmentRepository.GetAll().WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.StopDate >= input.Time1)
                        .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.StopDate <= input.Time2); ;
            //排序
            query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            //获取总数
            var tasksCount = query.Count();
            //默认的分页方式
            //var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            //ABP提供了扩展方法PageBy分页方式
            var taskList = query.PageBy(input).ToList(); 

            return new PagedResultDto<StopEquipmentListDto>(
             tasksCount,
             taskList.Select(item =>
             {
                 var dto = item.MapTo<StopEquipmentListDto>();
                 return dto;
             }).ToList());




            ////初步过滤
            //var query = _partRepository.GetAll();
            ////排序
            //query = !string.IsNullOrEmpty(input.Sorting) ? query.OrderBy(x => x.Id) : query.OrderByDescending(t => t.CreationTime);
            ////获取总数
            //var tasksCount = query.Count();
            ////默认的分页方式
            ////var taskList = query.Skip(input.SkipCount).Take(input.MaxResultCount).ToList();
            ////ABP提供了扩展方法PageBy分页方式
            //var taskList = query.PageBy(input).ToList();
            //return new PagedResultDto<PartListDto>(tasksCount, taskList.MapTo<List<PartListDto>>());
        }
        /// <summary>
        /// 根据查询条件获取停机信息分页列表
        /// </summary>
        public async Task<PagedResultDto<StopEquipmentListDto>> GetPagedStopEquipmentsAsync(GetStopEquipmentInput input)
{
			
    var query = _stopEquipmentRepositoryAsNoTrack;
    //TODO:根据传入的参数添加过滤条件

    var stopEquipmentCount = await query.CountAsync();

    var stopEquipments = await query
   
    .PageBy(input)
    .ToListAsync();

    var stopEquipmentListDtos = stopEquipments.MapTo<List<StopEquipmentListDto>>();
    return new PagedResultDto<StopEquipmentListDto>(
    stopEquipmentCount,
    stopEquipmentListDtos
    );
        }

        /// <summary>
    /// 通过Id获取停机信息信息进行编辑或修改 
    /// </summary> 
   public async Task<GetStopEquipmentForEditOutput> GetStopEquipmentForEditAsync(NullableIdDto<int> input)
{
    var output = new GetStopEquipmentForEditOutput();

            StopEquipmentEditDto stopEquipmentEditDto;

    if(input.Id.HasValue)
	{
    var entity = await _stopEquipmentRepository.GetAsync(input.Id.Value);
                stopEquipmentEditDto = entity.MapTo<StopEquipmentEditDto>();
    }
	else 
	{
                stopEquipmentEditDto = new StopEquipmentEditDto();
}

output.StopEquipment = stopEquipmentEditDto;
	return output;
    }

   


    /// <summary>
    /// 通过指定id获取停机信息ListDto信息
    /// </summary>
    public async Task<StopEquipmentListDto> GetStopEquipmentByIdAsync(EntityDto<int> input)
{
    var entity = await _stopEquipmentRepository.GetAsync(input.Id);

    return entity.MapTo<StopEquipmentListDto>();
    }




        public async Task CreateOrUpdateStopEquipmentAsync(CreateOrUpdateStopEquipmentInput input)
        {
            try
            {
                if (input.StopEquipmentEditDto.Id.HasValue)
                {
                    await UpdateStopEquipmentAsync(input.StopEquipmentEditDto);
                }
                else
                {
                    await CreateStopEquipmentAsync(input.StopEquipmentEditDto);
                }
            }catch (Exception ex)
            {

            }
            
        }


        /// <summary>
        /// 新增或更改停机信息
        /// </summary>


        /// <summary>
        /// 新增停机信息
        /// </summary>
        [AbpAuthorize(AppPermissions.Pages_StopEquipment_CreateStopEquipment)]	 
        public virtual async Task<StopEquipmentEditDto> CreateStopEquipmentAsync(StopEquipmentEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增
            
                var entity = input.MapTo<StopEquipment>();

                entity = await _stopEquipmentRepository.InsertAsync(entity);
                return entity.MapTo<StopEquipmentEditDto>();
           
          
        }

     

    /// <summary>
    /// 编辑停机信息
    /// </summary>
	      [AbpAuthorize(AppPermissions.Pages_StopEquipment_EditStopEquipment)]
         public virtual async Task UpdateStopEquipmentAsync(StopEquipmentEditDto input)
{
    //TODO:更新前的逻辑判断，是否允许更新

    var entity = await _stopEquipmentRepository.GetAsync(input.Id.Value);
	input.MapTo(entity);

    await _stopEquipmentRepository.UpdateAsync(entity);
    }

    /// <summary>
    /// 删除停机信息
    /// </summary>
	    [AbpAuthorize(AppPermissions.Pages_StopEquipment_DeleteStopEquipment)]
         public async Task DeleteStopEquipmentAsync(EntityDto<int> input)
{
    //TODO:删除前的逻辑判断，是否允许删除
    await _stopEquipmentRepository.DeleteAsync(input.Id);
    }

        /// <summary>
    /// 批量删除停机信息
    /// </summary>
	    [AbpAuthorize(AppPermissions.Pages_StopEquipment_DeleteStopEquipment)]
         public async Task BatchDeleteStopEquipmentAsync(List<int> input)
{
    //TODO:批量删除前的逻辑判断，是否允许删除
    await _stopEquipmentRepository.DeleteAsync(s=>input.Contains(s.Id));
    }

        #endregion

        #region 停机信息的Excel导出功能


        public async Task<FileDto> GetStopEquipmentToExcel(GetStopEquipmentInput input)
        {
            var entities = await _stopEquipmentRepository.GetAll()


                       .WhereIf(!input.Time1.ToString().IsNullOrEmpty(), u => u.StopDate >= input.Time1)
                       .WhereIf(!input.Time2.ToString().IsNullOrEmpty(), u => u.StopDate <= input.Time2)
                        .ToListAsync();



            var dtos = entities.MapTo<List<StopEquipmentListDto>>();

            var fileDto = _stopEquipmentExcelExporter.ExportStopEquipmentsToFile(dtos);




            return fileDto;
        }


        #endregion










    }
}
